<?
require("/var/www/html/new/d/common/auth.php");
require("/var/www/html/new/d/class/OrderRecord.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];
$errCode = array(
    '118'=>'终端信息出错，请联系技术人员[118]',
    '146'=>'终端信息出错，请联系技术人员[146]',
);
switch($_REQUEST['from']){
    case "index_code":
        $url="http://localhost/open/qrMIX?did=".$_SESSION['memberID'];
        if($p=$_REQUEST['p']){
            $url.="&p=$p";
        }
        if($d=$_REQUEST['d']){
            $url.="&d=$d";
        }
        if($t=$_REQUEST['t']){
            $t=urlencode($t);
            $url.="&t=$t";
        }
        $code_img=file_get_contents($url);
        echo $code_img;
        exit;
        break;
    case "logout":
        $sub_domain = $_SESSION['is_sub_domain'];
        // echo $sub_domain;
        session_destroy();
        // print_r($_SERVER);
        if($_SERVER['SERVER_NAME']!='www.12301.cc'){
            if(strpos($_SERVER['SCRIPT_URL'],'/new/d/')!==false)
                exit('{"outcome":2,"link":"http://'.$_SERVER['SERVER_NAME'].'/new/d/dlogin.html"}');
            exit('{"outcome":2,"link":"http://'.$_SERVER['SERVER_NAME'].($sub_domain>0?'/new/d':'').'/dlogin.html"}');
        }
        exit('{"outcome":1}');
        break;
    case "order_cancel":
        $ordernum=$_REQUEST['ordernum'];
        $or=new OrderRecord($ordernum,$s);
        $orders=$or->info();
        $concat = $_REQUEST['concat'] ? $_REQUEST['concat'] : 0;
        // 记录访问信息
        include_once("/var/www/html/new/d/class/AccessRecord.php");
        if(AccessRecord::insert(1,"退款|$ordernum|{$_SESSION['memberID']}")){
            $result = array();
            try{
                $result = $or->cancel($memberID,$concat);
            }catch(Exception $e){
                $out=array("outcome"=>-1,"msg"=>$e->getMessage());
                exit(json_encode($out));
            }
            if($result['code']==100){
                $msg="您".$orders['mainOrder']->UUltitle.$orders['mainOrder']->UUttitle."的订单".(string)$orders['mainOrder']->UUordernum."已被取消。【票付通】";
                $s->Send_SMS_System_Fee((string)$orders['mainOrder']->UUordertel,$msg,(string)$orders['mainOrder']->UUaid,(string)$orders['mainOrder']->UUordernum);
                $out=array("outcome"=>1);
                if($_SESSION['dtype']==6){
                    $daction="取消订单($ordernum)";
                    $str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
                    $GLOBALS['le']->query($str);
                }
            }else{
                $out=array("outcome"=>-1,"msg"=>$errCode[$result['msg']]);
            }
        }else{
            $out=array("outcome"=>-1,"msg"=>'操作信息记录失败');
        }

        echo json_encode($out);
        break;
    case "order_resend":
        $ordernum=$_REQUEST['ordernum'];
        $or=new OrderRecord($ordernum,$s);
        $orders=$or->info();
        if((string)$orders['mainOrder']->UUremsg>=3){
            $out=array("outcome"=>-1,"msg"=>"短信重发次数不能超过2次");
        }else{
            if($or->resendMsg($orders)){
                $out=array("outcome"=>1);
                if($_SESSION['dtype']==6){
                    $daction="重发订单短信($ordernum)";
                    $str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
                    $GLOBALS['le']->query($str);
                }
            }else{
                $out=array("outcome"=>-1);
            }
        }
        echo json_encode($out);
        break;
    case "order_alter":
        $out=array("outcome"=>1,"msg"=>"");
        $ordernum=$_REQUEST['ordernum'];
        $begintime=$_REQUEST['begintime'];
        $tids=$_REQUEST['tids'];

        $or=new OrderRecord($ordernum,$s);
        $info = $or->info();
        $info= (count($tids)>1) ? $or->info(0, 1):$or->info();
        if($info['mainOrder']->UUorder_confirm==5){
            exit('{"outcome":-1,"msg":"您的订单已经修改过一次，无法再次修改"}');
        }
        $orders=array(0=>$info['mainOrder']);
        // $orders=array_merge($orders,$info['childOrder']);

        /*修改游玩日期*/
        if(substr((string)$info['mainOrder']->UUbegintime,0,10)!=$begintime){
            require_once("../class/ProductInfo.php");
            foreach($orders as $row){
                $p=new ProductInfo($s,(string)$row->UUpid,(string)$row->UUaid);

                if($row->UUtprice!=$p->tPrice((string)$row->UUmid,$begintime))
                    exit('{"outcome":-1,"msg":"您选择的游玩日期与当前售价不同，不能修改！您可取消订单，重新下单"}');
            }
            if($begintime<substr((string)$info['mainOrder']->UUordertime,0,10))
                exit('{"outcome":-1,"msg":"游玩日期不能小于下单时间"}');
            if(!$or->changeBegintime($begintime,$memberID))
                exit('{"outcome":-1,"msg":"修改游玩日期失败"}');
            else
                $out['msg'].="游玩日期修改成功 ";
        }

        /*修改门票数*/
        $allcancel=1;
        foreach($tids as $tid=>$newtnum){
            if($newtnum==0){
                exit('{"outcome":-1,"msg":"不能将订单门票数改为0，请通过取消操作"}');
            }
        }
        foreach($orders as $row){
            if($tids[(string)$row->UUtid]){
                if($row->UUtnum==$tids[(string)$row->UUtid]) continue;
                if($row->UUtnum<$tids[(string)$row->UUtid]){
                    exit('{"outcome":-1,"msg":"不能增加订单门票数"}');
                }
                $result = $or->changeTnum((string)$row->UUtid,$tids[(string)$row->UUtid],$memberID,$info);
                if($result['code']!=100){
                    exit('{"outcome":-1,"msg":"修改门票数失败，'.$errCode[$result["msg"]].'"}');
                }else{
                    $out['msg'].="门票数修改成功 ";
                }
            }
        }
        if($out['msg']==""){
            $out['msg']="你未对订单做出变更";$out['outcome']=-1;
        }else{
            //$msg.="您".$info['mainOrder']->UUltitle.$info['mainOrder']->UUttitle."的订单".(string)$info['mainOrder']->UUordernum."已被修改。请查收新的短信";	//$s->Send_SMS_System_Fee((string)$info['mainOrder']->UUordertel,$msg,(string)$info['mainOrder']->UUaid,(string)$info['mainOrder']->UUordernum);
            if($_SESSION['dtype']==6){
                $daction="修改订单($ordernum)";
                $str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
                $GLOBALS['le']->query($str);
            }
            // 查询是否发送短信

            $sql='SELECT sendVoucher FROM uu_land_f WHERE tid="'.(string)$info['mainOrder']->UUtid.'" LIMIT 1';
            $GLOBALS['le']->query($sql);
            $GLOBALS['le']->fetch_assoc();
            if(!$GLOBALS['le']->f('sendVoucher')){
                $or->resendMsg();
            }
        }
        echo json_encode($out);
        break;
    case "orderAlterHtml":
        $ordernum=$_REQUEST['ordernum'];
        $or=new OrderRecord($ordernum,$s);
        $orders= ($_REQUEST['concat']==1)? $or->info(0,1):$or->info();
        if($orders['mainOrder']->UUp_type=='F'){// 套票只允许修改主票订单
            $orders['childOrder']=array();
        }
        /*
		<tr>
            <th>游玩日期</th>
            <td><input type="text" class="inputT" name="begintime" value="<?=$orders['mainOrder']->UUbegintime?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/></td>
        </tr>
        */
        ?>

        <form class="changetnum" action="/var/www/html/new/d/call/handle.php">
            <h3><?=$orders['mainOrder']->UUltitle?>-修改订单</h3>
            <p>(请输入小于原数量并且大于0的数字)</p>
            <input type="hidden" name="ordernum" value="<?=$orders['mainOrder']->UUordernum?>"/>
            <table>
                <input type="hidden" class="inputT" name="begintime" value="<?=$orders['mainOrder']->UUbegintime?>"/>
                <tr>
                    <th><?=$orders['mainOrder']->UUttitle?></th>
                    <td><input type="text" class="inputT" name="tids[<?=$orders['mainOrder']->UUtid?>]" value="<?=$orders['mainOrder']->UUtnum?>"/></td>
                </tr>
                <?foreach($orders['childOrder'] as $row){?>
                    <tr>
                        <th><?=$row->UUttitle?></th>
                        <td><input type="text" class="inputT" name="tids[<?=$row->UUtid?>]" value="<?=$row->UUtnum?>"/></td>
                    </tr>
                <?}?>
            </table>
            <div>
                <input type="submit" class="submit btn" value="确 定"/>
                <input type="button" class="cancel btn" value="取 消"/>
            </div>
        </form>
        <?
        break;
    case "withdraw_card"://添加提现账号
        $key = array_shift(array_keys($_POST));
        if (empty($_POST)) {
            exit('{"outcome":-1,"msg":"数据为空！"}');
        }
        $account_info = explode('|', $_POST[$key]);
        if ($account_info[3]!=$_SESSION['dname'] ) {
            exit('{"outcome":-1,"msg":"银行账号开户姓名必须与公司名称（'
                .$_SESSION['dname'].'）一致;\n如需修改，请联系客服。"}');
        }
        $str="update pft_member set $key='{$_POST[$key]}' where id=$memberID limit 1";
        $GLOBALS['le']->query($str);
        echo '{"outcome":1}';
        return true;
    case "withdraw_dele"://删除提现账号
        $bank_account=mysql_real_escape_string($_REQUEST['bankaccount']);
        $str="update pft_member set $bank_account='' where id=$memberID limit 1";
        $GLOBALS['le']->query($str);
        echo '{"outcome":1}';
        return true;
        break;
    case "withdraw_apply"://提现申请
        if($_REQUEST['money']<200){
            exit('{"outcome":-1,"msg":"转账金额不能小于200元"}');
        }
        $money=abs($_REQUEST['money'])*100;
        include_once("../class/Member.php");
        $m=new Member($s,$memberID);
        $m_info=$m->info();
        $account=mysql_real_escape_string($_REQUEST['account']);
        if($account=="alipay"){
            $bank_name="支付宝";
            $bank_area_name="中国";
            $bank_accuont=$m_info['alipay'];
            $wd_name=$cardinfo[1];
        }else{
            $cardinfo=explode("|",$m_info[$account]);
            $bank_name=$cardinfo[0];
            $bank_area_name=$cardinfo[1];
            $bank_accuont=$cardinfo[2];
            $wd_name=$cardinfo[3];
        }
        $str="select id from pft_wd_cash where wd_status=0 AND fid={$m_info['id']}";
        $GLOBALS['le']->query($str);
        if($GLOBALS['le']->fetch_assoc()){
            exit('{"outcome":-1,"msg":"还有未完成的提现申请！"}');
        }
        if(!$bank_accuont){
            exit('{"outcome":-1,"msg":"提现账户不能为空"}');
        }
        if(($m->money()-$money)<0){
            exit('{"outcome":-1,"msg":"提现金额不能大于账户余额！"}');
        }

        //取当前提现费率 2015-03-03 by zax
        $sel_fee_bank = "select fee_bank from pft_member where id=$memberID limit 1";
        $GLOBALS['le']->query($sel_fee_bank);
        $GLOBALS['le']->fetch_assoc();
        $fee_bank_once = $GLOBALS['le']-> f('fee_bank');

        $wd_money=$money;
        $bank_name.="(".$bank_area_name.")";
        $str="insert pft_wd_cash set fid=$memberID,wd_name='$wd_name',wd_money=$wd_money,apply_time=now(),bank_name='$bank_name',bank_accuont='$bank_accuont',batchno='$batchno',memo='提现',fee_bank_once=$fee_bank_once";
        if($GLOBALS['le']->query($str)){
            if($s->PFT_Member_Fund_Modify($memberID,$memberID,$wd_money,1,0,NULL,6)){
                exit('{"outcome":1}');
            }
        }
        exit('{"outcome":-1}');
        break;
    case "withdraw_cancel"://取消提现申请
        $id=abs($_REQUEST['id']);
        $str="select * from pft_wd_cash where id=$id";
        $GLOBALS['le']->query($str);
        $w_info=$GLOBALS['le']->fetch_assoc();
        if($w_info['fid']!=$memberID) exit('{"outcome":-1,"msg":"非本人提现，不能取消"}');
        $sel_sta = "select wd_status from pft_wd_cash where id=$id and fid=$memberID";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $wd_status = $GLOBALS['le']->f('wd_status');
        if($wd_status!=0)
            exit('{"outcome":-1,"msg":"这笔提现申请已经被处理了"}');
        if($wd_status==3)
        {}
        $str="update pft_wd_cash set wd_status=3 where id=$id and fid=$memberID";
        $wd_money=$w_info['wd_money'];
        if($GLOBALS['le']->query($str)){
            if($s->PFT_Member_Fund_Modify($memberID,$memberID,$wd_money,0,0,NULL,12)){

                exit('{"outcome":1}');
            }
        }
        break;
    case "refund"://提现申请处理
        $id=abs($_REQUEST['id']);
        $str="select wd_status,wd_money,fid from pft_wd_cash where id=$id LIMIT 1";
        $GLOBALS['le']->query($str);
        $w_info=$GLOBALS['le']->fetch_assoc();
        if($w_info['wd_status']!='0'){
            exit('{"outcome":-1,"msg":"这笔提现申请已经被处理了"}');
        }
        if($_REQUEST['act']=="suc"){
            $trade_no=mysql_real_escape_string($_REQUEST['trade_no']);
            $str="update  pft_wd_cash set wd_status=1,batchno=$trade_no where id=$id limit 1";
            if($GLOBALS['le']->query($str))	exit('{"outcome":1}');
        }
        elseif($_REQUEST['act']=="fail"){
            $memo=mysql_real_escape_string($_REQUEST['memo']);
            $wd_money=$w_info['wd_money'];
            $fid=$w_info['fid'];
            $str="update  pft_wd_cash set wd_status=2,memo='$memo' where id=$id limit 1";
            if($GLOBALS['le']->query($str)){
                if($s->PFT_Member_Fund_Modify($fid,$memberID,$wd_money,0,0,NULL,6)){
                    exit('{"outcome":1}');
                }
            }
        }else{
            exit('{"outcome":-1,"msg":"未知操作"}');
        }
        break;
    case "settle_config"://结算配置
        $settleid=abs($_REQUEST['settleid']);
        $billdate=abs($_REQUEST['billdate']);
        if($billdate>31||$billdate<1){
            exit('{"outcome":-1,"msg":"账单日期非法"}');
        }
        $str="update uu_settle set billdate=$billdate where id=$settleid limit 1";
        $GLOBALS['le']->query($str);
        echo '{"outcome":1,"msg":"配置成功"}';
        break;
    case "bill_alter":
        $id=abs($_REQUEST['id']);
        $str="select * from uu_bill where id=$id";
        $GLOBALS['le']->query($str);
        $b_info=$GLOBALS['le']->fetch_assoc();
        if($_REQUEST['act']=="apply"){
            if($b_info['status']<>0){
                exit('{"outcome":-1,"msg":"你的账单已申请结算"}');
            }
            $str="update uu_bill set status=1 where id=$id limit 1";
            $GLOBALS['le']->query($str);
        }
        if($_REQUEST['act']=="cancel"){
            if($b_info['status']<>1){
                exit('{"outcome":-1,"msg":"结算中账单才能取消"}');
            }
            $str="update uu_bill set status=0 where id=$id limit 1";
            $GLOBALS['le']->query($str);
        }
        if($_REQUEST['act']=="complete"){
            if($b_info['status']==2){
                exit('{"outcome":-1,"msg":"账单已结算"}');
            }
            $str="update uu_bill set status=2 where id=$id limit 1";
            $GLOBALS['le']->query($str);
        }
        echo '{"outcome":1,"msg":"操作成功"}';
        break;
    case "orderView":
        $ordernum=$_REQUEST['ordernum'];

        $or=new OrderRecord($ordernum,$s);
        $orders=$or->info();
        $aids = explode(',',(string)$orders['mainOrder']->UUaids);
        if((int)$orders['mainOrder']->UUmid!=$memberID && (int)$orders['mainOrder']->UUaid!=$memberID && !in_array($memberID,$aids) &&$_SESSION['dtype']!=9 && strlen($_SESSION['account'])!=11){
            //TODO::暂时先改成散客身份都能看凭证码 鼓浪屿那边 散客购买的订单 购买者和供应者跟散客都没有关系 以后要改一下
            // if((string)$orders['mainOrder']->UUmid!=$memberID && $_SESSION['dtype']!=9){
            exit('{"outcome":-1,"msg":"你无查看凭证号权限"}');
        }
        if(is_numeric($outdate)){
            echo '{"outcome":-1,"msg":"获取凭证号失败"}';
        }else{
            $outdate=$s->Terminal_Code_Verify($ordernum,$memberID,1);
            $outdate=simplexml_load_string($outdate);
            $code=$outdate->Rec[0]->UUcode;
            //$msg="订单号：".$ordernum." 凭证号：".$code;
            $msg="".$code;
            $array=array("outcome"=>1,"msg"=>$msg);
            echo json_encode($array);
        }
        break;
    //手机页电子凭证中心获取凭证码
    case 'appVCode':
        $ordernum=$_REQUEST['ordernum'];
        $outdate=$s->Terminal_Code_Verify($ordernum,$memberID,1);
        $or=new OrderRecord($ordernum,$s);
        $orders=$or->info();
        echo $_SESSION['dtype'];
        exit;
        if((string)$orders['mainOrder']->UUmid!=$memberID&&$_SESSION['dtype']!=9){
            echo '你无查看凭证号权限';
        }
        if(is_numeric($outdate)){
            echo '获取凭证号失败';
        }else{
            $outdate=simplexml_load_string($outdate);
            $code=$outdate->Rec[0]->UUcode;
            echo $code;
        }
        break;
    // 订单取消检测
    case 'cancelCheck':
        $ordernum=$_REQUEST['ordernum'];
        $or=new OrderRecord($ordernum,$s);
        $ordersInfo=$or->info();
        $orders = array(
            $ordersInfo['mainOrder']
        );
        $orders = array_merge($orders,$ordersInfo['childOrder']);
        $resArr = array('code'=>100);
        $cost = $mid = 0; // 取消总费用
        if($orders[0]->UUpaystatus!=2){
            foreach($orders as $order){
                // 不算套票的子票
                if((string)$order->UUpack_order>1) continue;
                $mid = (string)$order->UUmid;
                $tid = (string)$order->UUtid;
                $sql = "select reb,reb_type from uu_jq_ticket where id=$tid limit 1";
                $GLOBALS['le']->query($sql);
                $GLOBALS['le']->fetch_assoc();
                $reb = $GLOBALS['le']->f('reb');// 退款数值
                $reb_type = $GLOBALS['le']->f('reb_type');// 退款类型 0 百分比 1 具体指

                // 总价
                $tprice = (string)$order->UUtprice;
                $tnum = (string)$order->UUtnum;
                // 是否被修改了，取消按购买的票数
                $ordernum = (string)$order->UUordernum;
                $sql = "select pre_num from pft_order_num_modify where orderid='$ordernum' order by id asc limit 1";
                $GLOBALS['le']->query($sql);
                $GLOBALS['le']->fetch_assoc();
                if($GLOBALS['le']->f('pre_num')) $tnum = $GLOBALS['le']->f('pre_num');
                $total = $tprice * $tnum;
                $cost += ($reb_type==1) ? ($reb / 100 * $tnum) : $total * $reb/10000;
            }
            // 支付宝支付
            $refund_back = false;
            if($orders[0]->UUpmode==1){
                $sourceAid = (string)$orders[0]->UUaids;
                if($sourceAid==0){
                    $sourceAid = (string)$orders[0]->UUaid;
                }else{
                    $tmp = explode(',', $sourceAid);
                    $sourceAid = $tmp[0];
                }
                $sql="select refund_back from pft_member_extinfo where fid=$sourceAid limit 1";
                $GLOBALS['le']->query($sql);
                $GLOBALS['le']->fetch_assoc();
                if($GLOBALS['le']->f('refund_back')){
                    $refund_back = true;
                }
            }

            if(!$refund_back){
                // 获取分销商账户余额
                $result = $s->PFT_Member_Fund($mid,0);
                $object = simplexml_load_string($result);
                $balance = (string)$object->Rec->UUamoney;
                if($balance<0 && $cost>0){// 为 0 时给取消
                    $resArr['code'] = 101;
                    $resArr['msg'] = '账户余额不足';
                }
            }
        }

        $resArr['cost'] = $cost;
        $resArr['outcome'] = 1;
        echo json_encode($resArr);
        break;
    default:
        exit("非法操作");

}
?>